package com.example.adouble.cehuamenu.ui.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.adouble.cehuamenu.R;
import com.example.adouble.cehuamenu.mode.User;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * 注册
 */


public class RegisterActivity extends AppCompatActivity {

//    private CircleImageView head;
//    private BmobFile icon;
//    private File file;
    private EditText name,pwd,pwd1;
    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


//        Intent toGallery = new Intent(Intent.ACTION_GET_CONTENT);
//        toGallery.addCategory(Intent.CATEGORY_OPENABLE);
//        toGallery.setType("image/*");
//        startActivityForResult(toGallery,1);

        intiView();

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


    }

    /*
    *初始化
     */
    public void intiView(){
        name = findViewById(R.id.new_name);
        pwd = findViewById(R.id.new_pwd);
        pwd1 = findViewById(R.id.new_pwd1);
        back = findViewById(R.id.back);
    }

    /*
    *注册
     */
    public void register(View v){
        final String newname = name.getText().toString();
        final String newpwd = pwd.getText().toString();
        String newipwd1 = pwd1.getText().toString();
        check(newname,newpwd,newipwd1);

    }

    /*
    *检查是否用户已被注册
    * 已被注册就不能注册
     */
    public void check(final String name,final String pwd,final String pwd1){
        BmobQuery<User> bmobQuery = new BmobQuery<>();
        bmobQuery.addWhereEqualTo("name",name);
        bmobQuery.setLimit(1);
        bmobQuery.findObjects(new FindListener<User>() {
            @Override
            public void done(List<User> list, BmobException e) {
                if(e == null){
                    if(list.size()>0){
                        Toast.makeText(RegisterActivity.this, "用户已被注册", Toast.LENGTH_SHORT).show();
                    }else{
                        if(name.isEmpty() || pwd.isEmpty() || pwd1.isEmpty()){
                            Toast.makeText(RegisterActivity.this,"你还有未输入项",Toast.LENGTH_SHORT).show();

                        }else {
                            if (pwd.equals(pwd1)) {
                                User newuser = new User();
                                newuser.setName(name);
                                newuser.setPwd(pwd);
                                newuser.save(new SaveListener() {
                                    @Override
                                    public void done(Object o, BmobException e) {
                                        if (e == null) {
                                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                            finish();
                                        } else {
                                            Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            } else {
                                Toast.makeText(RegisterActivity.this, "两次输入密码不同", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                }else{
                    Toast.makeText(RegisterActivity.this, "请重试", Toast.LENGTH_SHORT).show();
                }
            }
        });


    }

//    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if(requestCode == 1){
//            Uri uri = data.getData();
//            file = new File(PathGetter.getPath(this,uri));
//            icon = new BmobFile(file);
//            try {
//                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),uri);
//                head.setImageBitmap(bitmap);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }

}
